
import java.util.Stack;


public class BalancedSymbols  {

	static char matchingSymbol(char sym) {
		switch (sym) {
		case ')':
			return '(';
		case ']':
			return '[';
		case '}':
			return '{';
		default:
			throw new IllegalArgumentException("invalid symbol: " + sym);
		}
	}

	public static void main(String[] args) {
		String symbols = args[0];
		Stack stack = new Stack();

		for (int i=0; i < symbols.length(); ++i) {
			char sym = symbols.charAt(i);
			switch (sym) {
			case '(':
			case '[':
			case '{':
				stack.push(new Character(sym));
				break;
			case ')':
			case ']':
			case '}':
				if (stack.empty()) {
					System.out.println("extraneous symbols: " + 
									   symbols.substring(i));
					return;
				}
				else {
					char top = ((Character)stack.pop()).charValue();
					if (top != matchingSymbol(sym)) {
						System.out.println("mismatch found: " +
										   top + "  " + sym);
						return;
					}
				}
				break;
			default:
				System.out.println("invalid symbol: " + sym);
				return;
			}
		}

		if (!stack.empty()) {
			StringBuffer unmatched = new StringBuffer();
			while (!stack.empty()) {
				char top = ((Character)stack.pop()).charValue();
				unmatched.insert(0, top);
			}

			System.out.println("unmatched symbols: " + unmatched.toString());
			return;
		}

		System.out.println("string is balanced");
	}

}
